package net.gooduo.spring_boot_template.config;


import net.gooduo.spring_boot_template.exception.HttpException;
import net.gooduo.spring_boot_template.http.HttpResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalControllerAdvice {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public HttpResult methodArgumentNotValidException(MethodArgumentNotValidException e) {
        return new HttpResult(1000, e.getBindingResult().getAllErrors().get(0).getDefaultMessage(), null);
    }

    @ExceptionHandler(HttpException.class)
    @ResponseBody
    public HttpResult exception(HttpException e) {
        return new HttpResult(e.getCode(), e.getMsg(), null);
    }
//
//    @ExceptionHandler(MaxUploadSizeExceededException.class)
//    @ResponseBody
//    public ApiResult maxUploadSizeExceededException(MaxUploadSizeExceededException e) {
//        return ApiResult.error(ExceptionEnum.UPLOAD_FILE_TOO_LARGE.getCode(), ExceptionEnum.UPLOAD_FILE_TOO_LARGE.getMsg());
//    }
}